package com.itheima.question.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.dto.PageDto;
import com.itheima.pojo.dto.QuestionDto;
import com.itheima.pojo.pojo.Module;
import com.itheima.pojo.pojo.Question;
import com.itheima.pojo.pojo.Role;
import com.itheima.pojo.vo.ModuleVo;
import com.itheima.pojo.vo.PageVo;
import com.itheima.pojo.vo.QuestionVo;
import com.itheima.pojo.vo.RoleVo;
import com.itheima.question.serivce.IQuestionSerivce;
import com.itheima.question.mapper.QuestionMapper;
import com.itheima.swagger.contants.RedisConstants;
import com.itheima.swagger.utils.TextSafe;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class QuestionServiceImpl implements IQuestionSerivce {
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Value("${fileServerUrl}")
    private String filePath;

    @Override
    public Boolean addQuestion(QuestionDto questionDto, HttpSession session) throws Exception {
        Question question=new Question();
        BeanUtils.copyProperties(questionDto,question);

        Boolean flag=questionMapper.insert(question)>0?true:false;
        if(flag==true){
            session.setAttribute("question_id",question.getId());
            //TODO 将问题id存入redis缓存中
            String qid=question.getId();
            redisTemplate.opsForSet().add(RedisConstants.AUDIT_QUESTION,qid);

            String pid=question.getPicture();
            redisTemplate.opsForSet().add(RedisConstants.USE_PIC,pid.replace(filePath,""));
        }
        else{
            return false;
        }
        return flag;
    }

    @Override
    public PageVo<QuestionVo> queryByPage(PageDto pageDto) {
        IPage<Question> page = new Page<>(pageDto.getCurrentPage(),pageDto.getPageSize());

        QueryWrapper<Question> queryWrapper = null;
        if(pageDto.getUsername() != null){
            queryWrapper = new QueryWrapper<>();
            queryWrapper.like("company_name",pageDto.getUsername());
            queryWrapper.eq("state",pageDto.getStatus());
        }

        IPage<Question> questionIPage = questionMapper.selectPage(page, queryWrapper);

        List<QuestionVo> collect = getQuestionVos(questionIPage.getRecords());
        return new PageVo<QuestionVo>(collect,(int)questionIPage.getTotal());
    }

    @Override
    public QuestionVo queryById(String id) {
        Question question = questionMapper.selectById(id);
        QuestionVo questionVo=new QuestionVo();
        BeanUtils.copyProperties(question,questionVo);
        return questionVo;
    }

    private List<QuestionVo> getQuestionVos(List<Question> questionIPage) {
        if (questionIPage == null) {
            return new ArrayList<>();
        }
        return questionIPage.stream().map(question -> {
            QuestionVo questionVo = new QuestionVo();
            BeanUtils.copyProperties(question, questionVo);
            return questionVo;
        }).collect(Collectors.toList());
    }
}
